<?php

namespace App\Http\Controllers\Backend\Message;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\Message\MessageTemplateIndexRequest;
use App\Http\Requests\Backend\Message\MessageTemplateStoreRequest;
use App\Http\Requests\Backend\Message\MessageTemplateUpdateRequest;
use App\Models\MessageTemplate;

class MessageTemplateController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:backend');
    }

    /**
     * Description:消息模板列表
     * Author: Sam
     * DateTime: 2019/1/10 12:40
     * @param MessageTemplateIndexRequest $request
     * @return mixed
     */
    public function index(MessageTemplateIndexRequest $request)
    {
        $page_size = $request->get('page_size',null);
        if($page_size){
            $list = MessageTemplate::orderByDesc('created_at')->paginate($page_size);
        }else{
            $list = MessageTemplate::orderByDesc('created_at')->get();
        }
        return success($list);
    }


    /**
     * Description:新建消息模板
     * Author: Sam
     * DateTime: 2019/1/10 12:40
     * @param MessageTemplateStoreRequest $request
     * @return mixed
     */
    public function store(MessageTemplateStoreRequest $request)
    {
        $params = $this->extractInputFromRules($request,$request->rules());
        $params['url'] = $params['url']??' ';
        MessageTemplate::create($params);
        return success([]);
    }

    /**
     * Description:获取单个消息模板
     * Author: Sam
     * DateTime: 2019/1/10 17:43
     * @param MessageTemplate $messageTemplate
     * @return mixed
     */
    public function show(MessageTemplate $messageTemplate)
    {
        return success($messageTemplate);
    }


    /**
     * Description:修改消息模板
     * Author: Sam
     * DateTime: 2019/1/10 17:53
     * @param MessageTemplateUpdateRequest $request
     * @param MessageTemplate $messageTemplate
     * @return mixed
     */
    public function update(MessageTemplateUpdateRequest $request, MessageTemplate $messageTemplate)
    {
        $params = $this->extractInputFromRules($request,$request->rules());
        $params['url'] = $params['url']??' ';
        $messageTemplate->updateLogic($params);
        return success([]);
    }

    /**
     * Description:删除消息模板
     * Author: Sam
     * DateTime: 2019/1/10 14:51
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        MessageTemplate::destroy($id);
        return success([]);
    }
}
